﻿using System;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using XiAnAirport.BUS;
using XiAnAirport.Models;

namespace XiAnAirport.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    [Authorize]
    public class MenuFOptionController : ControllerBase
    {
        public IHttpContextAccessor _contextAccessor;
        private JwtSettings _jwtSettings;
        public MenuFOptionController(IHttpContextAccessor contextAccessor, IOptions<JwtSettings> jwtSettingsAccesser)
        {
            _contextAccessor = contextAccessor;
            _jwtSettings = jwtSettingsAccesser.Value;
        }
        /// <summary>
        /// 新增菜单
        /// </summary>
        /// <param name="menuinfo">菜单信息</param>
        /// <returns>success</returns>
        [HttpPost]
        [Route("AddMenu")]
        public ResponseMessage AddMenu(Menu menuinfo)
        {
            MenuFOptionBUS BUS = new MenuFOptionBUS();
            ResponseMessage message = new ResponseMessage();
            try
            {
                string username = HttpContext.Session.GetString("UserName").ToString();
                menuinfo.creationPerson = username;
                message = BUS.AddMenu(menuinfo);
                if (message.errcode == 0)
                {
                    LogBUS logBUS = new LogBUS();
                    OperationLog operationLog = new OperationLog();
                    operationLog.operation = "新增菜单" + menuinfo.name;
                    operationLog.operatorName = username;
                    logBUS.InsertOperationLog(operationLog);
                }
            }
            catch (Exception e)
            {
                message.errcode = 3;
                message.errmsg = "新增菜单失败";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "新增菜单";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }

        /// <summary>
        /// 编辑菜单
        /// </summary>
        /// <param name="menuinfo">菜单信息</param>
        /// <returns>success</returns>
        [HttpPost]
        [Route("UpdateMenu")]
        public ResponseMessage UpdateMenu(Menu menuinfo)
        {
            MenuFOptionBUS BUS = new MenuFOptionBUS();
            ResponseMessage message = new ResponseMessage();
            try
            {
                message = BUS.EditMenu(menuinfo);
                if (message.errcode == 0)
                {
                    string username = HttpContext.Session.GetString("UserName").ToString();
                    LogBUS logBUS = new LogBUS();
                    OperationLog operationLog = new OperationLog();
                    operationLog.operation = "编辑菜单" + menuinfo.name;
                    operationLog.operatorName = username;
                    logBUS.InsertOperationLog(operationLog);
                }
            }
            catch (Exception e)
            {
                message.errcode = 3;
                message.errmsg = "编辑菜单失败";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "编辑菜单";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }


        /// <summary>
        /// 删除菜单
        /// </summary>
        /// <param name="menuinfo">菜单信息</param>
        /// <returns>success</returns>
        [HttpPost]
        [Route("DelMenu")]
        public ResponseMessage DelMenu(Menu menuinfo)
        {
            MenuFOptionBUS BUS = new MenuFOptionBUS();
            ResponseMessage message = new ResponseMessage();
            try
            {
                message = BUS.DelMenu(menuinfo.id);
                if (message.errcode == 0)
                {
                    string username = HttpContext.Session.GetString("UserName").ToString();
                    LogBUS logBUS = new LogBUS();
                    OperationLog operationLog = new OperationLog();
                    operationLog.operation = "删除菜单" + menuinfo.name;
                    operationLog.operatorName = username;
                    logBUS.InsertOperationLog(operationLog);
                }
            }
            catch (Exception e)
            {
                message.errcode = 3;
                message.errmsg = "删除菜单失败";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "删除菜单";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }

        /// <summary>
        /// 查询菜单
        /// </summary>
        /// <param name="menuinfo">菜单信息</param>
        /// <returns>success</returns>
        [HttpPost]
        [Route("QueryMenu")]
        public ResponseMessage QueryMenu(Menu menuinfo)
        {
            ResponseMessage message = new ResponseMessage();
            MenuFOptionBUS BUS = new MenuFOptionBUS();
            try
            {
                message = BUS.QueryMenu(menuinfo);
            }
            catch (Exception e)
            {
                message.errcode = 3;
                message.errmsg = "查询菜单失败";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "查询菜单";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }

        /// <summary>
        /// 新增菜单功能按钮
        /// </summary>
        /// <param name="foptioninfo">功能信息</param>
        /// <returns>success</returns>
        [HttpPost]
        [Route("AddFOption")]
        public ResponseMessage AddFOption(FOption foptioninfo)
        {
            MenuFOptionBUS BUS = new MenuFOptionBUS();
            ResponseMessage message = new ResponseMessage();
            try
            {
                string username = HttpContext.Session.GetString("UserName").ToString();
                foptioninfo.creationPerson = username;
                message = BUS.AddFOption(foptioninfo);
                if (message.errcode == 0)
                {
                    LogBUS logBUS = new LogBUS();
                    OperationLog operationLog = new OperationLog();
                    operationLog.operation = "新增菜单功能按钮" + foptioninfo.functionName;
                    operationLog.operatorName = username;
                    logBUS.InsertOperationLog(operationLog);
                }
            }
            catch (Exception e)
            {
                message.errcode = 3;
                message.errmsg = "新增菜单功能按钮失败";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "新增菜单功能按钮";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }


        /// <summary>
        /// 修改菜单功能按钮
        /// </summary>
        /// <param name="foptioninfo">功能信息</param>
        /// <returns>success</returns>
        [HttpPost]
        [Route("UpdateFOption")]
        public ResponseMessage UpdateFOption(FOption foptioninfo)
        {
            MenuFOptionBUS BUS = new MenuFOptionBUS();
            ResponseMessage message = new ResponseMessage();
            try
            {
                message = BUS.EditFOption(foptioninfo);
                if (message.errcode == 0)
                {
                    string username = HttpContext.Session.GetString("UserName").ToString();
                    LogBUS logBUS = new LogBUS();
                    OperationLog operationLog = new OperationLog();
                    operationLog.operation = "修改菜单功能按钮" + foptioninfo.functionName;
                    operationLog.operatorName = username;
                    logBUS.InsertOperationLog(operationLog);
                }
            }
            catch (Exception e)
            {
                message.errcode = 3;
                message.errmsg = "修改菜单功能按钮失败";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "修改菜单功能按钮";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }

        /// <summary>
        /// 删除菜单功能按钮
        /// </summary>
        /// <param name="foptioninfo">功能信息</param>
        /// <returns>success</returns>
        [HttpPost]
        [Route("DelFOption")]
        public ResponseMessage DelFOption(FOption foptioninfo)
        {
            MenuFOptionBUS BUS = new MenuFOptionBUS();
            ResponseMessage message = new ResponseMessage();
            try
            {
                message = BUS.DelFOption(foptioninfo.id);
                if (message.errcode == 0)
                {
                    string username = HttpContext.Session.GetString("UserName").ToString();
                    LogBUS logBUS = new LogBUS();
                    OperationLog operationLog = new OperationLog();
                    operationLog.operation = "删除菜单功能按钮" + foptioninfo.functionName;
                    operationLog.operatorName = username;
                    logBUS.InsertOperationLog(operationLog);
                }
            }
            catch (Exception e)
            {
                message.errcode = 3;
                message.errmsg = "删除菜单功能按钮失败";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "删除菜单功能按钮";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }

        /// <summary>
        /// 查询权限菜单
        /// </summary>
        /// <param name="userinfo">用户信息</param>
        /// <returns>success</returns>
        [HttpPost]
        [Route("QueryMenuByRole")]
        public ResponseMessage QueryMenuByRole(UserInfo userinfo)
        {
            ResponseMessage message = new ResponseMessage();
            MenuFOptionBUS BUS = new MenuFOptionBUS();
            try
            {
                string aa = _contextAccessor.HttpContext.Session.GetString("UserName");
                if (_contextAccessor.HttpContext.Session.GetString("UserName") == "Fstar1017!")
                {
                    userinfo.username = "fstar";
                }
                message = BUS.QueryMenuByRole(userinfo);
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = "查询菜单信息失败";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "查询菜单";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }

        /// <summary>
        /// 载入菜单信息
        /// </summary>
        /// <param name="menuinfo">菜单信息</param>
        /// <returns>Menu对象</returns>
        [HttpPost]
        [Route("LoadMenu")]
        public ResponseMessage LoadMenu(Menu menuinfo)
        {
            MenuFOptionBUS BUS = new MenuFOptionBUS();
            ResponseMessage message = new ResponseMessage();
            try
            {
                message = BUS.LoadMenu(menuinfo.id);
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = "载入菜单信息失败";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "载入菜单信息";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }

        /// <summary>
        /// 载入菜单功能按钮信息
        /// </summary>
        /// <param name="optionid">功能ID</param>
        /// <returns>Menu对象</returns>
        [HttpPost]
        [Route("LoadFOption")]
        public ResponseMessage LoadFOption(FOption optioninfo)
        {
            MenuFOptionBUS BUS = new MenuFOptionBUS();
            ResponseMessage message = new ResponseMessage();
            try
            {
                message = BUS.LoadFOption(optioninfo.id);
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = "载入菜单功能按钮信息失败";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "载入菜单功能按钮";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }

        /// <summary>
        /// 查询权限所有功能
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("QueryAllOptionByRoleID")]
        public ResponseMessage QueryAllOptionByRoleID(UserInfo useinfo)
        {
            ResponseMessage message = new ResponseMessage();
            MenuFOptionBUS BUS = new MenuFOptionBUS();
            try
            {
                message = BUS.QueryAllOption(useinfo);
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = "查询所有功能失败";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "查询权限所有功能";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }

        /// <summary>
        /// 查询所有功能
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("QueryAllMenuOption")]
        public ResponseMessage QueryAllMenuOption()
        {
            ResponseMessage message = new ResponseMessage();
            MenuFOptionBUS BUS = new MenuFOptionBUS();
            try
            {
                message = BUS.QueryAllMenuOption();
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = "查询所有功能失败";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "查询所有功能失败";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }

        /// <summary>
        /// 查询指定菜单下用户功能按钮
        /// </summary>
        /// <param name="userid">用户ID</param>
        /// <param name="url">菜单链接</param>
        /// <returns>success</returns>
        [HttpPost]
        [Route("QueryOptionByUserID")]
        public ResponseMessage QueryOptionByUserID(int userid, string url)
        {
            ResponseMessage message = new ResponseMessage();
            MenuFOptionBUS BUS = new MenuFOptionBUS();
            try
            {
                message = BUS.QueryOptionByUserID(userid, url);
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = "查询指定菜单下用户功能按钮失败";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "查询指定菜单下用户功能按钮";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }

        /// <summary>
        /// 查询指定菜单下所有功能按钮
        /// </summary>
        /// <param name="userid">用户ID</param>
        /// <param name="url">菜单链接</param>
        /// <returns>success</returns>
        [HttpPost]
        [Route("QueryOptionByMenu")]
        public ResponseMessage QueryOptionByMenu(int menuid)
        {
            ResponseMessage message = new ResponseMessage();
            MenuFOptionBUS BUS = new MenuFOptionBUS();
            try
            {
                message = BUS.QueryOptionByMenuID(menuid);
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = "查询功能按钮失败";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "查询指定菜单下所有功能按钮";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }

        /// <summary>
        /// 删除菜单
        /// </summary>
        /// <param name="menuinfo">菜单信息</param>
        /// <returns>success</returns>
        [HttpPost]
        [Route("BatchDelMenu")]
        public ResponseMessage BatchDelMenu(string ids)
        {
            MenuFOptionBUS BUS = new MenuFOptionBUS();
            ResponseMessage message = new ResponseMessage();
            try
            {
                message = BUS.BatchDelMenu(ids);
                if (message.errcode == 0)
                {
                    string username = HttpContext.Session.GetString("UserName").ToString();
                    LogBUS logBUS = new LogBUS();
                    OperationLog operationLog = new OperationLog();
                    operationLog.operation = "批量删除菜单功能按钮" +ids;
                    operationLog.operatorName = username;
                    logBUS.InsertOperationLog(operationLog);
                }
            }
            catch (Exception e)
            {
                message.errcode = 3;
                message.errmsg = "批量删除菜单失败";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "批量删除菜单";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }
    }
}
